<?php

App::uses('AppController', 'Controller');

/**
 * Posts Controller
 *
 * @property Post $Post
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 */
class PostsController extends AppController
{

    public function beforeFilter()
    {
        parent::beforeFilter();
        $this->layout = 'admin';
    }

    /**
     * Components
     *
     * @var array
     */
    public $helpers = array('TinymceElfinder.TinymceElfinder');
    public $components = array('Paginator', 'Session', 'ConvertDate','PregImage', 'TinymceElfinder.TinymceElfinder');

    public function elfinder()
    {
        $this->TinymceElfinder->elfinder();
    }

    public function connector()
    {
        $this->TinymceElfinder->connector();
    }

    /**
     * index method
     *
     * @return void
     */
    public function index()
    {
        $this->Post->recursive = 0;
        $this->set('posts', $this->Paginator->paginate());
        $this->set('dated', $this->ConvertDate->convertdate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null)
    {
        if (!$this->Post->exists($id))
        {
            $this->Session->setFlash('', 'notfound');
        } else
        {
            $this->loadModel('Category');
            $sql = "SELECT * FROM tb_categories AS Category, tb_posts AS Post WHERE Post.id = {$id} AND Post.id_category = Category.id";
            $this->set('posts', $this->Post->query($sql));
        }
//        debug($this->Post->query($sql));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add()
    {
        if ($this->request->is('post'))
        {
            $this->Post->create();
            $data = $this->request->data;
            $data['Post']['date'] = $this->ConvertDate->convertdate();
            $data['Post']['img'] = $this->PregImage->getSrc($data['Post']['img']);
            if ($this->Post->save($data))
            {
                $this->Session->setFlash(__('Thêm mới bài viết thành công!'), 'alert-success');
                return $this->redirect(array('action' => 'index'));
            } else
            {
                $this->Session->setFlash(__('Lỗi không thể thêm bài viết!'), 'alert-danger');
            }
        }
        $this->loadModel('Category');
        $categories = $this->Category->find('list', array('fields' => array('id', 'name_category')));
        $this->set('categories', $categories);
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null)
    {
        if (!$this->Post->exists($id))
        {
            $this->Session->setFlash('', 'notfound');
        } else
        {
            if ($this->request->is(array('post', 'put')))
            {
                $data = $this->request->data;
                $data['Post']['id'] = $id;
                $data['Post']['date'] = $this->ConvertDate->convertdate();
                $data['Post']['img'] = $this->PregImage->getSrc($data['Post']['img']);
                if ($this->Post->save($data))
                {
                    $this->Session->setFlash(__('Chỉnh sửa bài viết thành công!'), 'alert-success');
                    return $this->redirect(array('action' => 'index'));
                } else
                {
                    $this->Session->setFlash(__('Lỗi chỉnh sửa bài viết!, Xin vui lòng thử lại!'), 'alert-danger');
                }
            } else
            {
                $options = array('conditions' => array('Post.' . $this->Post->primaryKey => $id));
                $this->request->data = $this->Post->find('first', $options);
            }
            $this->loadModel('Category');
            $categories = $this->Category->find('list', array('fields' => array('id', 'name_category')));
            $this->set('categories', $categories);
        }
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null)
    {
        $this->Post->id = $id;
        if (!$this->Post->exists())
        {
            throw new NotFoundException(__('Invalid post'));
        }
        $this->request->allowMethod('post', 'delete');
        if ($this->Post->delete())
        {
            $this->Session->setFlash(__('Xóa thành công.'), 'alert-success');
        } else
        {
            $this->Session->setFlash(__('Xóa không thành công! Vui lòng thử lại!.'), 'alert-danger');
        }
        return $this->redirect(array('action' => 'index'));
    }

}
